package cn.com.nes.site.service.workOrder.impl;

import cn.com.nes.common.util.XzqhUtils;
import cn.com.nes.mybatis.agent.ope.entity.WorkOrderRejectionInfo;
import cn.com.nes.mybatis.agent.ope.mapper.WorkOrderRejectionInfoMapper;
import cn.com.nes.site.entity.em.WorkOrderRejectionTypeEnum;
import cn.com.nes.site.service.system.user.SystemUserService;
import cn.com.nes.site.service.workOrder.WorkOrderRejectionInfoService;
import cn.com.nes.site.service.workOrder.bo.GetWorkOrderRejectionInfoBO;
import cn.com.nes.site.service.workOrder.bo.InitRejectionInfoBO;
import cn.com.nes.site.service.workOrder.bo.WorkOrderRejectionInfoBO;
import cn.com.nes.site.service.workOrder.dto.WorkOrderRejectionInfoDTO;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 工单拒单记录 Service
 *
 * @Author: ShenWei
 * @Date: 2024/09/04/9:37
 */
@Slf4j
@Service
public class WorkOrderRejectionInfoServiceImpl extends ServiceImpl<WorkOrderRejectionInfoMapper, WorkOrderRejectionInfo> implements WorkOrderRejectionInfoService {

    @Resource
    private SystemUserService systemUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertWorkOrderRejectionInfo(WorkOrderRejectionInfoBO bo) {
        if (ObjectUtil.isNull(bo.getWorkOrderId())) {
            log.error("workOrderId must not null");
            return;
        }
        if (ObjectUtil.isNull(bo.getRemark())) {
            log.error("remark must not null");
            return;
        }
        if (ObjectUtil.isNull(bo.getType())) {
            log.error("type must not null");
            return;
        }

        // 标识当前工单的历史拒单记录为非最新
        QueryWrapper<WorkOrderRejectionInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("work_order_id", bo.getWorkOrderId());
        wrapper.eq("latest", true);
        wrapper.eq("deleted", false);

        WorkOrderRejectionInfo updateWorkOrderRejectionInfo = new WorkOrderRejectionInfo();
        updateWorkOrderRejectionInfo.setLatest(false);
        updateWorkOrderRejectionInfo.setGmtModified(bo.getOperateDate());
        updateWorkOrderRejectionInfo.setGmtModifiedId(bo.getOperateUserId());

        this.baseMapper.update(updateWorkOrderRejectionInfo, wrapper);

        // 插入新的拒单记录
        WorkOrderRejectionInfo workOrderRejectionInfo = new WorkOrderRejectionInfo();
        workOrderRejectionInfo.setWorkOrderId(bo.getWorkOrderId());
        workOrderRejectionInfo.setRemark(bo.getRemark());
        workOrderRejectionInfo.setLatest(true);
        workOrderRejectionInfo.setType(bo.getType());
        workOrderRejectionInfo.setDeleted(false);
        workOrderRejectionInfo.setGmtCreate(bo.getOperateDate());
        workOrderRejectionInfo.setGmtCreateId(bo.getOperateUserId());
        workOrderRejectionInfo.setGmtModified(bo.getOperateDate());
        workOrderRejectionInfo.setGmtModifiedId(bo.getOperateUserId());

        Integer rows = this.baseMapper.insert(workOrderRejectionInfo);
        if (rows != 1) {
            log.error("添加拒单记录失败,workOrderId:{}", bo.getWorkOrderId());
        }
    }

    @Override
    public Page<WorkOrderRejectionInfoDTO> getWorkOrderRejectionInfoPage(GetWorkOrderRejectionInfoBO bo) {
        if (StrUtil.isNotBlank(bo.getAreaCode())) {
            bo.setAreaCode(XzqhUtils.getRegionCodePrefix(bo.getAreaCode()));
        }

        Page<WorkOrderRejectionInfoDTO> page = new Page<>(bo.getPageid(), bo.getPagesize());
        List<WorkOrderRejectionInfoDTO> workOrderRejectionInfoDTOList = this.baseMapper.getWorkOrderRejectionInfoPage(page, bo);
        for (WorkOrderRejectionInfoDTO item : workOrderRejectionInfoDTOList) {
            item.setTypeName(WorkOrderRejectionTypeEnum.getVal(item.getType()));
        }
        page.setRecords(workOrderRejectionInfoDTOList);
        return page;
    }

    @Override
    public void initRejectionInfo(InitRejectionInfoBO initRejectionInfoBO) {
        if (ObjectUtil.isNull(initRejectionInfoBO.getWorkOrderId())) {
            log.error("安装工单id为空,不用废弃任何拒单记录");
            return;
        }

        try {
            QueryWrapper<WorkOrderRejectionInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("work_order_id", initRejectionInfoBO.getWorkOrderId());
            queryWrapper.eq("latest", true);
            List<WorkOrderRejectionInfo> workOrderRejectionInfoList = this.baseMapper.selectList(queryWrapper);
            if (CollectionUtil.isEmpty(workOrderRejectionInfoList)) {
                log.info("工单{}没有任何需要废弃的拒单记录", initRejectionInfoBO.getWorkOrderId());
                return;
            }
            for (WorkOrderRejectionInfo workOrderRejectionInfo : workOrderRejectionInfoList) {
                workOrderRejectionInfo.setLatest(false);
                workOrderRejectionInfo.setGmtModified(initRejectionInfoBO.getOperateDate());
                workOrderRejectionInfo.setGmtModifiedId(initRejectionInfoBO.getOperateUserId());

                Integer rows = this.baseMapper.updateById(workOrderRejectionInfo);
                if (1 != rows) {
                    log.error("安装工单【{}】废弃{}号拒单记录失败", initRejectionInfoBO.getWorkOrderId(), workOrderRejectionInfo.getId());
                }
            }
        } catch (Exception e) {
            log.error("废弃拒单记录异常,异常信息:{}", e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public Map<Long, Long> queryRejectionCountMap(Set<Long> workOrderIdSet) {
        if (CollectionUtil.isEmpty(workOrderIdSet)) {
            return new HashMap<>();
        }

        List<Map<String, Long>> result = this.baseMapper.queryRejectionCountMap(workOrderIdSet);
        Map<Long, Long> workOrderProjectIdMap = new HashMap<>();
        for (Map<String, Long> map : result) {
            workOrderProjectIdMap.put(map.get("workOrderId"), map.get("count"));
        }

        return workOrderProjectIdMap;
    }
}
